<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;
use think\facade\Db;
use app\admin\model\SystemGroupAdmin;
use util\Token;
use app\admin\model\SystemAdmin;
/**
 * Yeji控制器
 */
class Middleground extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\common\model\Olduser();
    }

    
    /**
     * 列表
     */
    public function index()
    { 
        //切换数据库查询
        //方法1
        // $query = Db::connect('OLD')->table('cmf_user')->select();
        // $query = table('system_admin')->select();
        //方法2 model里添加一行 protected $connection = 'OLD';
        $token = $this->request->header('token');
        $admin = Token::admin($token);
        $get = $this->request->get();
        $data['code'] = 1;
        $data['date'] = 1;
        $data['msg'] = '参数错误 即将关闭';
        if(empty($admin)){
            if(empty($get['id']) || empty($get['type'])){
                return json($data);
            }
        }
        if(!empty($admin)){
            if(empty($get['id']) || empty($get['type'])){
                $data['date'] = 4;
                $data['msg'] = '已登录';
                return json($data);
            }
        }
        //登录新账号
        if(empty($admin) || $admin['old_userid'] !== $get['id']){
                $user = Db::connect('OLD')
                    ->table('cmf_user')
                    ->where('id',$get['id'])
                    ->find();
                if(empty($user)){
                    $data['msg'] = '找不到用户信息';
                    return json($data);
                }
                $new_userid = table('system_admin')
                    ->where('old_userid',$user['id'])
                    ->find();
                if(!empty($new_userid)){
                    $userid = $new_userid['id'];
                }else{
                    $new_userid = table('system_admin')
                        ->where('nickname',$user['user_nickname'])
                        ->find();
                    if(!empty($new_userid)){
                        $update['old_userid'] = $user['id'];
                        table('system_admin_middleground')
                            ->where('id',$new_userid['id'])
                            ->update($update);
                    }else{
                        $insert_user['nickname'] = $user['user_nickname'];
                        $insert_user['username'] = $user['user_login'];
                        $insert_user['password'] = 'e10adc3949ba59abbe56e057f20f883e';
                        $insert_user['old_userid'] = $user['id'];
                        $insert_id = table('system_admin')
                            ->insertGetId($insert_user);
                        $userid = $insert_id;
                    }  
                }
                if(!empty($admin) && $admin['old_userid'] !== $get['id']){
                    //新用户登录
                    $admin = SystemAdmin::where(['id' => $userid])->find();
                    $is_keeplogin = true;
                    $group_ids = SystemGroupAdmin::where('admin_id', $userid)->column('group_id');
                    $admin['group_ids'] = $group_ids;
                    unset($admin['password']);
                    $token = Token::create($admin->toArray(), $is_keeplogin);
                }else{
                    //自动登录
                    $token = $this->request->header('token');
                    if(empty($token)){
                        $admin = SystemAdmin::where(['id' => $userid])->find();
                        $is_keeplogin = true;
                        $group_ids = SystemGroupAdmin::where('admin_id', $userid)->column('group_id');
                        $admin['group_ids'] = $group_ids;
                        unset($admin['password']);
                        $token = Token::create($admin->toArray(), $is_keeplogin);
                    }
                }
                
        }else{
            // halt($admin);
        }
        //已登录
        if($get['type'] == 0){
            $data['date'] = 4;
            $data['msg'] = '已登录';
            return json($data);
        }
        //业绩系统
        if($get['type'] == 1){
            $data['date'] = 2;
            $data['msg'] = $token;
            return json($data);
        }
        //考勤系统
        if($get['type'] == 2){
            $data['date'] = 3;
            $data['msg'] = $token;
            return json($data);
        }
    }

}